<!-- eslint-disable vue/multi-word-component-names -->
<template>
    <div>
  <el-breadcrumb separator-class="el-icon-arrow-right">
  <el-breadcrumb-item :to="{ path: '/rear-end/index' }">首页</el-breadcrumb-item>
  <el-breadcrumb-item>用户管理</el-breadcrumb-item>
  <el-breadcrumb-item>用户列表</el-breadcrumb-item>
</el-breadcrumb>
<el-card >
    <el-row :gutter="20">
    <el-col :span="8"><el-input placeholder="请输入内容" v-model="input3" class="input-with-select" clearable @clear="clearPage"  @keyup.enter.native="selectByusername">
    <el-button slot="append" icon="el-icon-search" @click.native="selectByusername"></el-button>
  </el-input>
</el-col>
<el-col :span="4">
  <el-button type="primary" @click="dialogVisible = true">添加用户</el-button>
  <el-dialog
  title="添加用户"
  :visible.sync="dialogVisible"
  width="50%"
  >
  <AddAdminTemplate ref="addAdminRef" :admin="admin" ></AddAdminTemplate>
  <span slot="footer" class="dialog-footer">
    <el-button @click="add(false)">取 消</el-button>
    <el-button type="primary" @click="add(true)">确 定</el-button>
  </span>
</el-dialog>
</el-col>
</el-row>
<el-row>
    <el-col>
        <el-table
    :data="tableData"
    style="width: 100%"
    border>
    <el-table-column
      type="index"
      align="center"
      width="50">
    </el-table-column>
    <el-table-column
      property="username"
      label="用户名"
      align="center"
      width="120">
    </el-table-column>
    
    <el-table-column
      property="password"
      label="密码"
      align="center"
      width="120">
    </el-table-column>
    <el-table-column
      property="phone"
      label="手机号"
      align="center"
      width="120">
    </el-table-column>
    <el-table-column
    align="center"
      property="type"
      :formatter="formattype"
      width="80"
      label="角色">
    </el-table-column>
   
    <el-table-column
      align="center"
      label="状态"
      width="80">
      <template slot-scope="scope">
      <el-switch
      :value="scope.row.status"
      :active-value="1"
      :inactive-value="0"
      active-color="#409eff"
      inactive-color="#ff4949"
      @click.native="updatestatus(scope.row,scope.$index)"
      v-if="scope.row.type!=1"
      ref="statusRef">
      </el-switch>
      </template>
    </el-table-column>
    <el-table-column
    align="center"
      property="createTime"
      :formatter="dateFormat"
      label="创建用户时间">
    </el-table-column>
    <el-table-column
      label="操作"

      align="center"
      width="120">
      <template slot-scope="scope" v-if="scope.row.type!=1">
        <el-dialog
          title="编辑用户"
          :visible.sync="dialogVisible1"
          width="50%"
          >
          <UpdateAdminTemplate ref="updateAdminRef"  ></UpdateAdminTemplate>
          <span slot="footer" class="dialog-footer">
            <el-button @click="update(false)">取 消</el-button>
            <el-button type="primary" @click="update(true)">确 定</el-button>
          </span>
        </el-dialog>
        <el-button type="primary" @click="a(scope.row)" icon="el-icon-edit" size="mini"></el-button>
        <el-button type="danger" @click="del(scope.row)" icon="el-icon-delete" size="mini"></el-button>
      </template>
    </el-table-column>
  </el-table>
    </el-col>
</el-row>
<!-- @size-change="handleSizeChange"
      @current-change="handleCurrentChange" -->
<el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[ 2, 5, 10,20]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
</el-card>
</div>
</template>

<script>
import '@/assets/css/global.css'
import AddAdminTemplate from '@/components/rear-end/user/AddAdminTemplate.vue';
import UpdateAdminTemplate from '@/components/rear-end/user/UpdateAdminTemplate.vue';
import loginAdmin from '@/js/loginAdmin.js';

export default {
    data() {
      return {
        admin:'',
        tableData: '',
        queryInfo:{
          //当前每页显示多少条数据
          pagesize:2,
          //当前页数
          pagenum:1
        },
        updatePage:{
          //当前每页显示多少条数据
          pagesize:2,
          //当前页数
          pagenum:1
        },
        total:'',
        currentRow: null,
        dialogVisible: false,
        dialogVisible1:false,
        input3:''
      }
    },
    components:{AddAdminTemplate,UpdateAdminTemplate},
    created(){
      this.selectadmin()
    },
    methods:{
      //用户列表查询
   async selectadmin(){
        const res = await this.$api.rear_end.allAdmin(this.queryInfo)
       this.tableData=res.data.data
       this.total=res.data.map.total
       loginAdmin.setAdmin(res.data.map.username,res.data.map.adminId,res.data.map.type,res.data.map.createAdminId)
     this.admin=loginAdmin.admin
      },
      //根据用户名查询数据
    async  selectByusername(){
        if(this.input3.trim()===''){
         this.$message.error('请输入用户名')
       }else{
        const selectByUsername={
           queryInfo:this.updatePage,
           admin:{
            username:this.input3
           }
            
          }
          const res = await this.$api.rear_end.selectAdmin(selectByUsername)
         if(res.data.code===1){
        this.queryInfo=this.updatePage
       this.tableData=res.data.data
       this.total=res.data.map.total
       loginAdmin.setAdmin(res.data.map.username,res.data.map.adminId,res.data.map.type,res.data.map.createAdminId)
       this.admin=loginAdmin.admin
       this.$forceUpdate()   
      }
        }

      },
     
      //删除用户信息
    async  del(row){
      this.$confirm(`是否删除该账号？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(async () => {
        const deladmin={
          adminId:row.adminId
        }
        const res = await this.$api.rear_end.delAdmin(deladmin)
        if(res.data.code===1){
          this.$message.success(res.data.data) 
          if(this.total<this.queryInfo.pagesize*this.queryInfo.pagenum&&this.queryInfo.pagenum>1){
           this.queryInfo.pagenum=this.queryInfo.pagenum-1
          } 
          this.selectadmin()
          this.$forceUpdate()
        }
        else{
          this.$message.error(res.data.msg)  
          
        }
      }).catch(() => {
        this.$message.error('取消删除')
      })
        
      },
      //添加用户提示框方法
  async add(b){
        if(b){
          const addbollen= await this.$refs.addAdminRef.addadmin()
          console.log(addbollen)
        if(addbollen){
          this.selectadmin()
        }
        }
        this.dialogVisible=false
      },
      a(admin){
        this.dialogVisible1=true
         this.$nextTick(()=>this.$refs.updateAdminRef.admin1(admin))
      },
      // 编辑用户信息
 async update(b){
        if(b){
        const updatebollen= await this.$refs.updateAdminRef.updateadmin()
        if(updatebollen){
          this.selectadmin()
        }
        }
        this.dialogVisible1=false
      },
      handleSizeChange(val) {
        const maxnum=Math.ceil(this.total/val)
        this.queryInfo.pagesize=val
        if(this.queryInfo.pagenum>maxnum){
      this.queryInfo.pagenum=maxnum
         }  
        
        if(this.input3.trim()!=''){
          if(this.updatePage.pagenum>maxnum){
            this.updatePage.pagenum=maxnum
         }  
          this.updatePage.pagesize=val
         this.selectByusername()
        }else{
        this.selectadmin()
        }
      
        
      },
      handleCurrentChange(val) {
        this.queryInfo.pagenum=val
        if(this.input3.trim()!=''){
          this.updatePage.pagenum=val
          this.selectByusername()
        }else{
        this.selectadmin()
        }
       
      },
      //修改用户状态
     async updatestatus(row){
        this.$confirm(`${!row.status==1 ? '开启' : '关闭'}账号, 是否继续？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(async () => {
        let data
        switch(row.status){
        case 1:data=0 
        break
        case 0:data=1 
        break
        }
        const admin={
          adminId:row.adminId,
          status:data
        }
        const res = await this.$api.rear_end.updateAdmin(admin)
        if(res.data.code===1){
          this.$message.success(res.data.data)  
          this.selectadmin()
        }
        else{
          this.$message.error(res.data.msg)  
          
        }
      }).catch(() => {
        this.$message.error('取消操作')
      })
      },
      //状态数据转换
      formattype(row){
       return row.type===1?'超级管理账号':(row.type===2?'管理账号':'普通账号')
      },
        /*日期处理*/
    dateFormat(row){
      return row.createTime.toLocaleString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
    },
    //清空查询名称初始page
    clearPage(){
      this.queryInfo.pagenum=1
      this.queryInfo.pagesize=2
      this.updatePage.pagenum=1
      this.updatePage.pagesize=2
      this.selectadmin()
    }

    }

}
</script>

<style lang="less" scoped>

</style>